package saucaca.Adapter;

import java.util.ArrayList;
import java.util.List;

import lemon.kenhnhacvn.BaseEngine;
import lemon.kenhnhacvn.MainActivity;
import lemon.kenhnhacvn.R;

import org.json.JSONException;

import saucaca.CustomView.CustomExpandableListView;
import saucaca.JSON.JsonReader;
import saucaca.JSON.WServiceLib;
import saucaca.Object.SongObject;
import saucaca.Util.CMD;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class XExpandableSongAdapter extends BaseExpandableListAdapter {
	private int[] groupStatus;
	ArrayList<SongObject> listSongs;
	CustomExpandableListView mExpandableListView;
	Context mContext;
	ImageLoader imageLoader = ImageLoader.getInstance();
	DisplayImageOptions options;
	boolean isHome = false;

	public XExpandableSongAdapter(Context pContext, CustomExpandableListView pExpandableListView, List<SongObject> listSongs) {
		options = new DisplayImageOptions.Builder().showImageOnFail(R.drawable.ic_default).showStubImage(R.drawable.ic_default).showImageForEmptyUri(R.drawable.ic_default)
				.cacheInMemory().cacheOnDisc().build();
		this.mContext = pContext;
		this.mExpandableListView = pExpandableListView;
		mExpandableListView.setGroupIndicator(mContext.getResources().getDrawable(R.drawable.expandlist_indicator));
		this.listSongs = (ArrayList<SongObject>) listSongs;
		groupStatus = new int[this.listSongs.size()];
		setListclick();
		setListEvent();
	}

	public XExpandableSongAdapter(Context pContext, CustomExpandableListView pExpandableListView, List<SongObject> listSongs, boolean isHome) {
		options = new DisplayImageOptions.Builder().showImageOnFail(R.drawable.ic_default).showStubImage(R.drawable.ic_default).showImageForEmptyUri(R.drawable.ic_default)
				.cacheInMemory().cacheOnDisc().build();
		this.mContext = pContext;
		this.mExpandableListView = pExpandableListView;
		mExpandableListView.setGroupIndicator(mContext.getResources().getDrawable(R.drawable.expandlist_indicator));
		this.listSongs = (ArrayList<SongObject>) listSongs;
		groupStatus = new int[this.listSongs.size()];
		setListclick();
		setListEvent();
		this.isHome = isHome;
	}

	private void setListclick() {
		mExpandableListView.setOnGroupClickListener(new OnGroupClickListener() {
			@Override
			public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
				if (true) {
					((MainActivity) mContext).showPanelControl(true);
					if (listSongs.get(groupPosition).getPATCH_FULL() != null && !listSongs.get(groupPosition).getPATCH_FULL().equals("")) {
						((MainActivity) mContext).SendPlaylistAndplay(listSongs, groupPosition);
					} else
						new GetFromServices().execute(listSongs.get(groupPosition).getSONG_ID());
					return true;
				}
				return false;
			}
		});
	}

	class GetFromServices extends AsyncTask<String, String, String> {

		@Override
		protected String doInBackground(String... params) {
			String str = WServiceLib.idDetais(CMD.CMD_SONG_DETAIL, params[0]);
			BaseEngine.showLogformLongString(str);
			return str;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			if(result!=null)
			try {
				ArrayList<SongObject> listsongs = JsonReader.readSongdetail(result);
				((MainActivity) mContext).SendPlaylistAndplay(listsongs, 0);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	}

	public void expandGroup(int groupPosition) {
		if (mExpandableListView.isGroupExpanded(groupPosition))
			mExpandableListView.collapseGroup(groupPosition);
		else
			mExpandableListView.expandGroup(groupPosition);
	}

	private void setListEvent() {
		mExpandableListView.setOnGroupExpandListener(new OnGroupExpandListener() {
			@Override
			public void onGroupExpand(int arg0) {
				// TODO Auto-generated method stub
				groupStatus[arg0] = 1;
				// int len = mGroupCollection.size();
				int len = listSongs.size();
				for (int i = 0; i < len; i++) {
					if (i != arg0) {
						mExpandableListView.collapseGroup(i);
					}
				}
			}
		});
		mExpandableListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
			@Override
			public void onGroupCollapse(int arg0) {
				// TODO Auto-generated method stub
				groupStatus[arg0] = 0;
			}
		});
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return 1;
	}

	@Override
	public Object getGroup(int arg0) {
		// TODO Auto-generated method stub
		// return mGroupCollection.get(arg0);
		return listSongs.get(arg0);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		// return mGroupCollection.size();
		return listSongs.size();
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, final ViewGroup parent) {
		final GroupHolder groupHolder;
		if (convertView == null) {
			if (isHome) {
				convertView = LayoutInflater.from(mContext).inflate(R.layout.item_song_online_home, null);
				groupHolder = new GroupHolder();
				groupHolder.backgroud = (RelativeLayout) convertView.findViewById(R.id.relativeGroup);
				groupHolder.artist = (TextView) convertView.findViewById(R.id.custom_baihat_item_artist);
				groupHolder.listent = (TextView) convertView.findViewById(R.id.custom_baihat_item_textluotnghe);
				groupHolder.title = (TextView) convertView.findViewById(R.id.custom_baihat_item_title);
				groupHolder.img = (ImageView) convertView.findViewById(R.id.custom_groupview_imageExpand);
				groupHolder.like = (TextView) convertView.findViewById(R.id.custom_baihat_item_textYeuthich);
			} else {
				convertView = LayoutInflater.from(mContext).inflate(R.layout.item_song_online, null);
				groupHolder = new GroupHolder();
				groupHolder.backgroud = (RelativeLayout) convertView.findViewById(R.id.relativeGroup);
				groupHolder.artist = (TextView) convertView.findViewById(R.id.custom_baihat_item_artist);
				groupHolder.listent = (TextView) convertView.findViewById(R.id.custom_baihat_item_textluotnghe);
				groupHolder.title = (TextView) convertView.findViewById(R.id.custom_baihat_item_title);
				groupHolder.img = (ImageView) convertView.findViewById(R.id.custom_groupview_imageExpand);
				groupHolder.imgAvatar = (ImageView) convertView.findViewById(R.id.custom_baihat_item_Avatar);
				groupHolder.like = (TextView) convertView.findViewById(R.id.custom_baihat_item_textYeuthich);
			}
			convertView.setTag(groupHolder);
		} else {
			groupHolder = (GroupHolder) convertView.getTag();
		}
		if (groupStatus[groupPosition] == 0) {
			// groupHolder.backgroud.setBackgroundResource(R.drawable.bg_ct);
			groupHolder.img.setImageResource(R.drawable.icon_bh_sh2);
		} else {
			// groupHolder.backgroud.setBackgroundResource(R.drawable.bg_ct_active);
			groupHolder.img.setImageResource(R.drawable.icon_bh_sh);
		}
		// groupHolder.artist.setText(Integer.toString(mGroupCollection.get(groupPosition).getId()));
		// groupHolder.listent.setText(Integer.toString(mGroupCollection.get(groupPosition).getId()));
		// groupHolder.title.setText(mGroupCollection.get(groupPosition).getName());

		groupHolder.img.setOnClickListener(new View.OnClickListener() {
			@SuppressLint("NewApi")
			@Override
			public void onClick(View v) {
				expandGroup(groupPosition);
			}
		});
		if (!isHome) {
			if (!listSongs.get(groupPosition).getAVATAR().equals(""))
				imageLoader.displayImage(listSongs.get(groupPosition).getAVATAR(), groupHolder.imgAvatar, options);
		}
		groupHolder.like.setText(listSongs.get(groupPosition).getISDOWN());
		groupHolder.artist.setText(listSongs.get(groupPosition).getNAME_SINGERS());
		groupHolder.listent.setText(listSongs.get(groupPosition).getISVIEW());
		groupHolder.title.setText(listSongs.get(groupPosition).getTXNAME());
		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_song_online_child_expand, null);
		}
		LinearLayout buttonAddplaylist = (LinearLayout) convertView.findViewById(R.id.custom_baihat_home_buttonAddplaylist);
		LinearLayout buttonDownload = (LinearLayout) convertView.findViewById(R.id.custom_baihat_home_buttonDownload);
		LinearLayout buttonRington = (LinearLayout) convertView.findViewById(R.id.custom_baihat_home_buttonRington);
		LinearLayout buttonShare = (LinearLayout) convertView.findViewById(R.id.custom_baihat_home_buttonShare);
		final int indexGroup = groupPosition;
		buttonAddplaylist.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// Toast.makeText(mContext,
				// "Selected: "+mGroupCollection.get(indexGroup).getName(),
				// Toast.LENGTH_SHORT).show();
				Toast.makeText(mContext, "Addplaylist: " + listSongs.get(indexGroup).getTXNAME(), Toast.LENGTH_SHORT).show();
			}
		});
		buttonDownload.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast.makeText(mContext, "Download: " + listSongs.get(indexGroup).getTXNAME(), Toast.LENGTH_SHORT).show();
			}
		});
		buttonRington.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast.makeText(mContext, "Rington: " + listSongs.get(indexGroup).getTXNAME(), Toast.LENGTH_SHORT).show();
			}
		});
		buttonShare.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast.makeText(mContext, "Share: " + listSongs.get(indexGroup).getTXNAME(), Toast.LENGTH_SHORT).show();
			}
		});

		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

	class GroupHolder {
		boolean isExpanded = false;
		RelativeLayout backgroud;
		ImageView img;
		ImageView imgAvatar;
		TextView title;
		TextView artist;
		TextView listent;
		TextView like;
	}

	class ChildHolder {
		ImageButton buttonAddplaylist;
		ImageButton buttonDownload;
		ImageButton buttonRington;
		ImageButton buttonShare;
	}

}
